/**
 * 处理一周的时间
 * @param {*} param 日期可以是时间对象,可以是YYYY-MM-DD的
*/
import {weekDateFnParam} from './interfaces';
export default function (param: weekDateFnParam = { date: new Date(), startWeek: 1, data: [], detailkey: 'date' }) {
    let { date, startWeek, data, detailkey } = param;
    let newDate = new Date(date);
    let year1 = newDate.getFullYear();
    let year2 = newDate.getFullYear();
    let month1 = newDate.getMonth();
    let month2 = newDate.getMonth();
    let date1 = newDate.getDate();
    let date2 = newDate.getDate();
    let weeks = [7, 1, 2, 3, 4, 5, 6];
    let week_k = newDate.getDay();
    let week = weeks[week_k];
    const isLeapYear = (year: number) => {
        if (((year % 4) == 0) && ((year % 100) != 0) || ((year % 400) == 0)) {
            return 1;
        } else {
            return 0;
        }
    }
    const addZero = (n: number) => {
        if (n < 10) {
            return '0' + n
        } else {
            return n;
        }
    }

    let start_i_minus = 1;
    if (startWeek == 1) {
        start_i_minus = 0;
    }
    if (startWeek == 7) {
        start_i_minus = 1;
    }
    if (startWeek != 7 && startWeek != 1) {
        throw new Error('startWeek只能传入1和7');//Uncaught Error: test
        return false;
    }

    const dates1 = [31, 28 + isLeapYear(year1), 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];       //所有月份的天数,这里条用的一个函数用于计算闰年
    const dates2 = [31, 28 + isLeapYear(year1), 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];       //所有月份的天数,这里条用的一个函数用于计算闰年
    let start = [];
    for (let i = 0; i < week + start_i_minus; i++) {
        let _week = null;
        if (startWeek == 1) {
            _week = week - i;
        } else {
            _week = (week - i) == 0 ? 7 : (week - i);
        }
        if (date1 < 1) {
            month1 = month1 - 1;
            if (month1 < 0) {
                month1 = 11;
                year1 = year1 - 1;
                dates1[1] = (28 + isLeapYear(year1));
            }
            date1 = dates1[month1];
        }
        start.push({
            day: date1,
            week: _week,
            month: month1,
            year: year1
        })
        date1 -= 1;
    }
    let end = [];
    for (let i = 1; i <= 7 - week - start_i_minus; i++) {
        let _week = week + i;
        date2 += 1;
        if (date2 > dates2[month2]) {
            month2 = month2 + 1;
            if (month2 > 11) {
                month2 = 0;
                year2 = year2 + 1;
                dates2[1] = (28 + isLeapYear(year2));
            }
            date2 = 1;
        }
        end.push({
            day: date2,
            week: _week,
            month: month2,
            year: year2
        })
    }
    let all: any = start.concat(end);
    all.forEach((v: any) => {
        v.dateStr = `${v.year}-${addZero((v.month + 1))}-${addZero(v.day)}`;
        data.forEach((item:any) => {
            if (item[detailkey] == v.dateStr) {
                v['detail'] = item;
            }
        })
    })
    if (startWeek == 1) {
        all.sort((a: any, b: any) => {
            return a.week - b.week;
        })
    }
    if (startWeek == 7) {
        all.sort((a: any, b: any) => {
            return b.week - a.week;
        }).sort((a: any, b: any) => {
            if (b.week != 7) {
                return a.week - b.week;
            }
        })
    }
    return all;
}